//
//  MenuView.m
//  Travel
//
//  Created by Hunk on 11-3-16.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MenuView.h"
#import <QuartzCore/QuartzCore.h>
#import "CategoryViewController.h"

#define RECT_IMAGEVIEW CGRectMake(140, 0, 180, 200)

#define SAFE_RELEASE(p) if(p){[p release]; p = nil;}

@implementation MenuView
@synthesize m_arrayMenuItem;
@synthesize m_pNavigationController;

- (id)initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
	{
		// Init data
		m_arrayMenuItem = [[NSArray alloc] init];
		
		// Image view
		UIImageView* imageView = [[UIImageView alloc] init];
		imageView.frame = RECT_IMAGEVIEW;
		[imageView setImage:[[UIImage imageNamed:@"PopoverTopArrowRight.png"] 
                             stretchableImageWithLeftCapWidth:14 topCapHeight:35]];
		[self addSubview:imageView];
		[imageView release];
		
		// TableView
		UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(146, 30, 168, 164) style:UITableViewStylePlain];
		tableView.delegate = self;
		tableView.dataSource = self;
		tableView.layer.cornerRadius = 8.0;
		tableView.layer.masksToBounds = YES;
		[self addSubview:tableView];
		[tableView release];
    }
    return self;
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [m_arrayMenuItem count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 40.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
	{
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    // Cell Background view
    UIView* cellBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 168, 40)];
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 168, 40)];
    [bgImageView setImage:[UIImage imageNamed:@"CellBackground.png"]];
    [cellBgView addSubview:bgImageView];
    [bgImageView release];
    [cell setBackgroundView:cellBgView];
    [cellBgView release];
    
	// Set Text label
    cell.textLabel.backgroundColor = [UIColor clearColor];
	cell.textLabel.text = [[m_arrayMenuItem objectAtIndex:indexPath.row] objectForKey:@"name"];
	cell.textLabel.textAlignment = UITextAlignmentCenter;
	
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	CategoryViewController* categoryViewController = [CategoryViewController alloc];
	
	categoryViewController.hidesBottomBarWhenPushed = YES;
	
	categoryViewController.title = [[m_arrayMenuItem objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    categoryViewController.categoryId = [[m_arrayMenuItem objectAtIndex:indexPath.row] objectForKey:@"categoryId"];
	
	[m_pNavigationController pushViewController:categoryViewController animated:YES];
	
	[categoryViewController release];
}

#pragma mark -
#pragma mark touchesEnded
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	CGPoint location = [touch locationInView:[touch view]];
	
	if(!CGRectContainsPoint(RECT_IMAGEVIEW, location))
	{
		[self removeFromSuperview];
		self = nil;
	}
}

- (void)dealloc 
{
    SAFE_RELEASE(m_arrayMenuItem)
    SAFE_RELEASE(m_pNavigationController)
	
    [super dealloc];
}


@end
